/*
* 时间：2021.07.22
* 代码来源：公众号【从0到1的点云】
* 功能：实现点云的求异操作
*/

#include <iostream>
#include <pcl/point_cloud.h>
#include <pcl/octree/octree.h>
#include <vector>
#include <ctime>
//函数用来生成随机点云
void random_cloud(pcl::PointCloud<pcl::PointXYZ>::Ptr &cloud, int number)
{
  srand((unsigned int)time(NULL));
  (*cloud).height = 1;//random cloud flag
  (*cloud).width= number;
  (*cloud).points.resize(((*cloud).height)*((*cloud).width));
  for (size_t i = 0; i < (*cloud).points.size(); i++)
  {
    (*cloud).points[i].x = 64.0f*rand() / (RAND_MAX + 1.0F);
    (*cloud).points[i].y = 64.0f*rand() / (RAND_MAX + 1.0F);
    (*cloud).points[i].z = 64.0f*rand() / (RAND_MAX + 1.0F);
  }
}
int main()
{
  float resolution = 32.0f;
  pcl::octree::OctreePointCloudChangeDetector<pcl::PointXYZ> octree(resolution);
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloudA(new pcl::PointCloud<pcl::PointXYZ>);
  random_cloud(cloudA, 128);
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloudB(new pcl::PointCloud<pcl::PointXYZ>);
  random_cloud(cloudB, 5000);
  octree.setInputCloud(cloudA);
  octree.addPointsFromInputCloud();
  octree.switchBuffers();//点云交换缓冲区，但a点云仍在八叉树内
  octree.setInputCloud(cloudB);
  octree.addPointsFromInputCloud();
  std::vector<int> new_point_index;
  octree.getPointIndicesFromNewVoxels(new_point_index);
  if (new_point_index.empty()!=0)
    {
    std::cout << "vector no points!"<<std::endl;
    }
   else
  {
    std::cout << "求异结果得到" << new_point_index.size() << "个点！" << std::endl;
  }
  return 0;
}